<?php

class Controller_Index extends Controller_Website
{

    public function before() {
        parent::before();
        $this->mod = Model::factory('directory');
        $this->man = Model::factory('manager');
        $this->section = 'index';
    }

	public function actionIndex()
	{

		$this->template->title = $this->settings['title'];
		$this->template->content = new View('index/index');
//        $this->addScript('/scripts/jquery.cycle.lite.js');
        $this->addScript('/scripts/jquery.cycle.all.js');

                $top_left = $this->manager->getMainPageImage(0);
                $top_right = $this->manager->getMainPageImage(1);
                $bottom_left = $this->manager->getMainPageImage(2);
                $bottom_right = $this->manager->getMainPageImage(3);
                
                $this->template->content->customImages=$this->mod->getImages('custom');
                
                $this->template->content->top_left = $top_left;
                $this->template->content->top_right = $top_right;
                $this->template->content->bottom_left = $bottom_left;
                $this->template->content->bottom_right = $bottom_right;

                
                $voteImages = $this->mod->getImages('vote'); 
                
                if(isset($_SESSION['identity'])){
                    $userId = $_SESSION['identity']['id'];
                    
                    foreach ($voteImages as $key=>$val){
                       $imageId = $voteImages[$key]['id']; 
                       $result = $this->manager->checkVoted($userId,$imageId);
                       if($result){
                           $voteImages[$key]['voted'] = 1;
                       }
                       else{
                           $voteImages[$key]['voted'] = 0;
                       }
                       $voteImages[$key]['votesQuantity'] = $this->manager->countVotes($imageId);
                    }
                }
                
                $this->template->content->voteImages=$voteImages;
	}

    public function actionVote(){
        
        $this->autoRender = FALSE;
        if(Request::$isAjax)
        {
            $response = array();
            if(!empty($_SESSION['identity']))
            {
                $pictureId = (is_numeric($_POST['id'])) ? trim($_POST['id']) : '';
                $userId = $_SESSION['identity']['id'];
                $result =$this->manager->addVote($userId,$pictureId);
                if($result){
                    $response['success'] = 'Yout vote has been added.Thank you!';
                    $response['result'] = $result;
                }
                else
                {
                    $response['error'] = 'You have already voted for this item';
                }


                
                
/*================ FirePHP TESTING*/            
    
//            FB::group('first group');
//                FB::log('hello this is fire php');
//                FB::warn('some warning');
//                FB::info('important info');
//                FB::error('this is error');
//            FB::groupEnd();
//
//            FB::table('some', $this->mod->getImages('custom'));  
                
/*================ FirePHP TESTING end*/    
            }
        }
    }
}